<template>
  <div>
    <vxe-toolbar id="toolbarId" :setting="{ storage: true }" export :refresh="{ query: findList }">
      <template v-if="!parentData.showInfo" v-slot:buttons>
        <el-button @click="funcFilter">查询</el-button>
        <el-button @click="funcAdd">新增</el-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      highlight-hover-row
      row-id="id"
      height="700"
      :data="tableData"
      :loading="loading"
      remote-sort
      @sort-change="sortList"
    >
      <vxe-table-column>
        <vxe-table-column field="consignorAddress" title="装货地址" min-width="200" sortable />
        <template v-slot:header>
          <el-cascader
            v-model="consignorAreaList"
            :options="parentData.areaOptions"
            :props="{ value: 'countyCode', label: 'countyName' }"
            clearable
            @change="changeConsignorAreaList"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="consignorDetailAddress" title="详细装货地址" min-width="100" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.consignorDetailAddress" placeholder="详细装货地址" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="consigneeAddress" title="收货地址" min-width="100" sortable />
        <template v-slot:header>
          <el-cascader
            v-model="consigneeAreaList"
            :options="parentData.areaOptions"
            :props="{ value: 'countyCode', label: 'countyName' }"
            clearable
            @change="changeConsigneeAreaList"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="consigneeDetailAddress" title="详细收货地址" min-width="100" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.consigneeDetailAddress" placeholder="详细收货地址" clearable />
        </template>
      </vxe-table-column>

      <vxe-table-column v-if="!parentData.showInfo" title="操作" width="100px" fixed="right">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="编辑" placement="top">
            <vxe-button type="text" @click="funcEdit(scope.row.code)">
              <i class="el-icon-edit" />
            </vxe-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="删除" placement="top">
            <vxe-button type="text" @click="funcDelete(scope.row.code)">
              <i class="el-icon-delete" />
            </vxe-button>
          </el-tooltip>
        </template>
      </vxe-table-column>
    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />
    <vxe-modal v-model="modelAddFlag" title="新增" width="800" height="500" resize>
      <compAdd
        :area-options="parentData.areaOptions"
        @fatherAddMethod="fatherAdd"
        @fatherCancelMethod="fatherCancel"
      />
    </vxe-modal>
    <vxe-modal v-model="modelEditFlag" title="编辑" width="800" height="500" resize>
      <compEdit
        ref="editRef"
        :area-options="parentData.areaOptions"
        @fatherEditMethod="fatherEdit"
        @fatherCancelMethod="fatherCancel"
      />
    </vxe-modal>
  </div>
</template>
<script>
import Vue from 'vue'
import 'xe-utils'
import VXETable from 'vxe-table'
import 'vxe-table/lib/index.css'
import XEUtils from 'xe-utils'
import VXEUtils from 'vxe-utils'
Vue.use(VXETable, VXEUtils, XEUtils, { mounts: ['cookie'] })
import compAdd from './components/commonRouteInfoAdd.vue'
import compEdit from './components/commonRouteInfoEdit.vue'
import { getCommonRouteInfoList, delCommonRouteInfo } from '@/api/shipment'
export default {
  name: 'DriverCommonRouteInfoList',
  components: {
    compAdd,
    compEdit
  },
  props: {
    parentData: {
      type: Object,
      default() {
        return {
          showInfo: false,
          areaOptions: []
        }
      }
    }
  },
  data() {
    return {
      message: '',
      loading: false,
      consignorAreaList: [],
      consigneeAreaList: [],
      tableData: [],
      modelAddFlag: false,
      modelEditFlag: false,
      copy: false,
      listQuery: {
        userCode: '',
        consignorProvinceCode: '',
        consignorCityCode: '',
        consignorCountyCode: '',
        consignorDetailAddress: '',
        consigneeProvinceCode: '',
        consigneeCityCode: '',
        consigneeCountyCode: '',
        consigneeDetailAddress: '',
        pageNum: 1,
        pageSize: 10,
        sortField: '',
        sortType: '',
        total: 0
      }
    }
  },
  created() {
    this.listQuery.userCode = this.$route.query.partyCode
    this.findList()
  },
  methods: {
    findList() {
      this.loading = true
      return new Promise(resolve => {
        getCommonRouteInfoList(this.listQuery).then(response => {
          this.tableData = response.data.records
          this.loading = false
          resolve();
        })
      })
    },
    changeStartDate(value) {
      this.listQuery.gmtCreatedStart = value + ' 00:00:00'
    },
    changeEndDate(value) {
      this.listQuery.gmtCreatedEnd = value + ' 23:59:59'
    },
    changeConsigneeAreaList(value) {
      this.listQuery.consigneeProvinceCode = value[0]
      this.listQuery.consigneeCityCode = value[1]
      this.listQuery.consigneeCountyCode = value[2]
    },
    changeConsignorAreaList(value) {
      this.listQuery.consignorProvinceCode = value[0]
      this.listQuery.consignorCityCode = value[1]
      this.listQuery.consignorCountyCode = value[2]
    },
    // 查询
    funcFilter() {
      this.listQuery.pageNum = 1;
      this.findList();
    },
    // 排序
    sortList({ column, property, order }) {
      var str
      switch (property) {
        case 'consignorAddress':
          str = 'consignorCountyCode'
          break
        case 'consigneeAddress':
          str = 'consigneeCountyCode'
          break
        default:
          str = property
          break
      }
      if (order) {
        this.listQuery.sortField = str
        this.listQuery.sortType = order
      } else {
        this.listQuery.sortField = ''
      }
      this.findList()
    },
    // 添加方法
    funcAdd() {
      this.modelAddFlag = true
    },
    // 修改方法
    funcEdit(code) {
      this.$refs.editRef.getInfo(code)
      this.modelEditFlag = true
    },
    // 删除方法
    funcDelete(codes) {
      this.$confirm('确认删除吗？')
        .then(_ => {
          delCommonRouteInfo({ codes }).then(response => {
            this.$message({ message: response.msg, type: 'success' })
            this.findList()
          })
        })
        .catch(_ => {})
    },
    // 增加组件的父方法
    fatherAdd() {
      this.modelAddFlag = false
      this.findList()
    },
    // 编辑组件的父方法
    fatherEdit() {
      this.modelEditFlag = false
      this.findList()
    },
    // 公共取消
    fatherCancel(modelFlag) {
      switch (modelFlag) {
        case 'add':
          this.modelAddFlag = false
          break
        case 'edit':
          this.modelEditFlag = false
          break
      }
    },
    // 分页handle
    handlePageChange({ currentPage, pageSize }) {
      this.listQuery.pageNum = currentPage
      this.listQuery.pageSize = pageSize
      this.findList()
    }
  }
}
</script>
<style lang="scss" scoped>
.el-cascader {
  width: 100%;
}
</style>
